MySQL-specific Geography Tasks
IN THIS PAGE
Description
MySQL examples for creating and dropping tables with geometry columns, inserting location data values, inserting line data values, and inserting polygon data values.
In the samples below, :SRID indicates the optional spatial reference identifier argument. For MySQL, the default is 4326.
Creating and Dropping Tables with Geometry Columns
Creating a table with geometry columns:
CREATE TABLE GeogTest ( KeyValue varchar(25) NOT NULL, Location GEOMETRY NOT NULL, PRIMARY KEY (KeyValue)) ENGINE = InnoDB
Dropping a table:
DROP TABLE GeogTest
Inserting Location Data Values
Inserting location data using Portable SQL Syntax:
INSERT INTO GeogTest (KeyValue, Location) values('Item1', GeogCreateLocation(-70, 42, :SRID))
Inserting location data using Native syntax:
INSERT INTO GeogTest (KeyValue, Location) VALUES ('Item1', PointFromText(CONCAT('POINT(', CAST( - 70 as CHAR(32)), ' ', CAST(42 as CHAR(32)), ')'), :SRID))
Inserting Line Data Values
Inserting line data values using Portable SQL Syntax:
INSERT INTO GeogTest (KeyValue, Location) values('Item2', GeogCreateLine(-70, 42, -70, 38, :SRID))
Inserting line data values using Native SQL Syntax:
INSERT INTO GeogTest (KeyValue, Location) VALUES ('Item2', LineStringFromText(CONCAT('LINESTRING(', CAST( - 70 as CHAR(32)), ' ' , CAST(42 as CHAR(32)), ', ', CAST( - 70 as CHAR(32)), ' ' , CAST(38 as CHAR(32)), ')'),:SRID))
Inserting Polygon Data Values
Inserting polygon values using Portable SQL Syntax:
INSERT INTO GeogTest (KeyValue, Location) values('Item3', GeogCreatePolygon(-70, 42, -70, 32, -60, 32, -60, 42, -70, 42, :SRID))
Inserting polygon values using Native SQL Syntax:
INSERT INTO GeogTest (KeyValue, Location) VALUES ('Item3', PolygonFromText( CONCAT('POLYGON((',CAST( - 70 as CHAR(32)), ' ', CAST(42 as CHAR(32)), ', ', CAST( - 70 as CHAR(32)), ' ', CAST(32 as CHAR(32)), ', ', CAST( - 60 as CHAR(32)), ' ', CAST(32 as CHAR(32)), ', ', CAST( - 60 as CHAR(32)), ' ', CAST(42 as CHAR(32)), ', ', CAST( - 70 as CHAR(32)), ' ', CAST(42 as CHAR(32)), '))'),:SRID))
See Also
- Geographic Databases
- Changes to SQL Objects for Geometry
- Portable SQL Functions for Geographies
- Common Geography Database Tasks
- Database-specific Geography Tasks
- DB2-specific Geography Tasks
- MySQL-specific Geography Tasks
- Oracle-specific Geography Tasks
- PostgreSQL-specific Geography Tasks
- SQL Server-specific Geography Tasks
- SQL Geography Examples